package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Zscq;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class ZscqForm extends BasePage implements Serializable {
    private GenericManager<Zscq, Long> zscqManager;
    private Zscq zscq = new Zscq();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setZscqManager(GenericManager<Zscq, Long> zscqManager) {
        this.zscqManager = zscqManager;
    }

    public Zscq getZscq() {
        return zscq;
    }

    public void setZscq(Zscq zscq) {
        this.zscq = zscq;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        zscqManager.remove(zscq.getId());
        addMessage("zscq.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            zscq = zscqManager.get(id);
        } else {
            zscq = new Zscq();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (zscq.getId() == null || zscq.getId() == 0);
        zscq.setJbqk( jbqk );
        zscq.setUsername(getRequest().getRemoteUser());
        zscq.setOptdate(new Date());
        zscq.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        zscqManager.save(zscq);

        String key = (isNew) ? "zscq.added" : "zscq.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = zscq.getId();
            return "edit";
        }
    }
} 